﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using Google.API.Search;
using System.Threading.Tasks;

namespace BD
{
    class BotDetector
    {

        private const string AccountKey = "pIMQpcOxYoe/2avZsE3xURv/0pFq+acGSPqyI/FY09o";
        
        public double Analyze(string searchstring)
        {
            string[] words = searchstring.Split(' ');
            if (words.Length <= 3)
            {
                return 0.5;
            }
            string query = "\"" + searchstring + "\"";
            string rootUrl = "https://api.datamarket.azure.com/Bing/Search";
            var bingContainer = new Bing.BingSearchContainer(new Uri(rootUrl));
            string market = "en-us";
            bingContainer.Credentials = new NetworkCredential(AccountKey, AccountKey);
            var webQuery = bingContainer.Web(query, null, null, market, null, null, null, null);
            webQuery = webQuery.AddQueryOption("$top", 50);
            //webQuery = webQuery.AddQueryOption("$skip", 50);
            var webResults = webQuery.Execute();
            int i = 0;            
            foreach (var result in webResults)
            {
                i++;                    
            }
            if (i >= 3)
            {
                return 1;
            }
            else if (i > 0)
                return 0.5;
            else
                return 0;
        }

    }
}
